home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / bluebook.zip / BINARY.ASM < prev    next >
Assembly Source File  |  1986-05-08  |  12KB  |  392 lines

  1.                                       COMMENT ~
  2. BINARY.ASM -- Binary Conversion Routines
  3.  
  4.    From `BLUEBOOK of ASSEMBLY ROUTINES for the IBM PC & XT'
  5.          by Christopher L. Morgan
  6.          Copyright (C) 1984 by The Waite Group, Inc.
  7.  
  8.       >>>>>  See BINARY.DOC for complete description of routines.  <<<<<
  9.  
  10.      Contents:
  11.      ---------
  12.      BIN16IN    --  Convert from ASCII Binary to 16-bit Binary
  13.      BIN16OUT    --  Convert from 16-bit Binary to ASCII Binary
  14.      BIN8OUT    --  Convert from 8-bit Binary to ASCII Binary
  15.      DEC16IN    --  Convert from ASCII Decimal to 16-bit Binary       
  16.      DEC16OUT    --  Convert from 16-bit Binary to ASCII Decimal
  17.      DEC8OUT    --  Convert from 8-bit Binary to ASCII Decimal
  18.      HEX16IN    --  Convert from ASCII Hexadecimal to 16-bit Binary
  19.      HEX16OUT    --  Convert from 16-bit Binary to ASCII Hexadecimal
  20.      HEX8OUT    --  Convert from 8-bit Binary to ASCII Hexadecimal
  21.      OCT16IN    --  Convert from ASCII Octal to 16-bit Binary
  22.      OCT16OUT    --  Convert from 16-bit Binary to ASCII Octal
  23.      OCT8OUT    --  Convert from 8-bit Binary to ASCII Octal
  24.  _____________________________________________________________________________ 
  25.  This data should be included in the source code calling these routines, and
  26.    this section then be commented out.                                        ~
  27. DATAS    SEGMENT    PUBLIC
  28.     TBUFF    DB    5 DUP(?)
  29. DATAS    ENDS 
  30. ;_____________________________________________________________________________ 
  31. ;        
  32. CODES    SEGMENT
  33.  
  34.     EXTRN    STDIN:FAR,STDOUT:FAR
  35.  
  36.     PUBLIC BIN16IN,BIN8OUT,BIN16OUT,OCT16IN,OCT8OUT,OCT16OUT
  37.     PUBLIC HEX16IN,HEX8OUT,HEX16OUT,DEC16IN,DEC8OUT,DEC16OUT
  38.  
  39. ASSUME CS:CODES,DS:DATAS
  40. ;_____________________________ I/O ROUTINES __________________________________
  41. ;
  42. ;Routine to convert from ASCII binary to internal 16-bit binary
  43. ;
  44. BIN16IN   PROC     FAR
  45.       PUSH     AX               ;Save registers
  46.        MOV     DX,0               ;Initialize DX as 0
  47. BIN16IN1:
  48.       CALL     STDIN               ;Digit comes in through AL
  49.       SUB     AL,30H            ;Subtract 30H
  50.       JL     BIN16IN2           ;Check if too low
  51.       CMP     AL,1
  52.       JG     BIN16IN2           ;Check if too high
  53.       CBW                   ;Convert to word
  54.       SAL     DX,1               ;Shift DX left once
  55.       ADD     DX,AX               ;Add in digit
  56.       JMP     BIN16IN1
  57. BIN16IN2:
  58.       POP     AX               ;Restore registers
  59.       RET                   ;Return
  60. BIN16IN   ENDP
  61. ;-----------------------------------------------------------------------------
  62. ;Routine to convert from internal 8-bit binary to ASCII binary
  63. ;
  64. BIN8OUT   PROC FAR
  65. ;
  66. ;A binary number is in DL
  67.       PUSH     CX               ;Save registers
  68.       PUSH     AX
  69.       MOV     CX,8               ;Loop for a count of 8
  70. BIN8OUT1:
  71.       ROL     DL,1               ;Rotate DL left once
  72.       MOV     AL,DL               ;Move into AL
  73.       AND     AL,1               ;Keep just digit
  74.       ADD     AL,30H            ;Add 30H to AL
  75.       CALL     STDOUT            ;Send it out
  76.       LOOP     BIN8OUT1
  77.       POP     AX               ;Restore registers
  78.       POP     CX
  79.       RET                   ;Return
  80. BIN8OUT   ENDP
  81. ;-----------------------------------------------------------------------------
  82. ;Routine to convert from internal 16-bit binary to ASCII binary
  83. ;
  84. BIN16OUT  PROC     FAR
  85. ;
  86. ;A binary number is in DX
  87.       PUSH     CX               ;Save registers
  88.       PUSH     AX
  89.       MOV     CX,16               ;Loop for a count of 16
  90. BIN16OUT1:
  91.       ROL     DX,1               ;Rotate DX left once
  92.       MOV     AL,DL               ;Move into AL
  93.       AND     AL,1               ;Keep just digit
  94.       ADD     AL,30H            ;Add 30H to AL
  95.       CALL     STDOUT            ;Send it out
  96.       LOOP     BIN16OUT1
  97.       POP     AX               ;Restore registers
  98.       POP     CX
  99.       RET                   ;Return
  100. BIN16OUT  ENDP
  101. ;-----------------------------------------------------------------------------
  102. ;Routine to convert from ASCII octal to internal 16-bit binary
  103. ;
  104. OCT16IN   PROC     FAR
  105.       PUSH     CX               ;Save registers
  106.       PUSH     AX
  107.       MOV     DX,0               ;Initialize DX as 0
  108. OCT16IN1:
  109.       CALL     STDIN               ;A digit comes in AL
  110.       SUB     AL,30H            ;Subtract 30H
  111.       JL     OCT16IN2           ;Check if too low
  112.       CMP     AL,7               ;
  113.       JG     OCT16IN2           ;Check if too high
  114.       CBW                   ;Convert to word
  115.       MOV     CL,3
  116.       SAL     DX,CL               ;Shift DX left once
  117.       ADD     DX,AX               ;Add in digit
  118.       JMP     OCT16IN1
  119. OCT16IN2:
  120.       POP     AX               ;Restore registers
  121.       POP     CX
  122.       RET                   ;Return
  123. OCT16IN   ENDP
  124. ;-----------------------------------------------------------------------------
  125. ;Routine to convert from internal 8-bit binary to ASCII octal
  126. ;
  127. OCT8OUT   PROC     FAR
  128. ;
  129. ;A binary number is in DL
  130.       PUSH     CX               ;Save registers
  131.       PUSH     AX
  132. ;
  133. ;First octal digit has only 2 bits
  134.       MOV     CL,2               ;For a count of 2
  135.       ROL     DL,CL               ;Rotate DL left
  136.       MOV     AL,DL               ;Move into AL
  137.       AND     AL,3               ;Keep just digit
  138.       ADD     AL,30H            ;Add 30H to AL
  139.       CALL     STDOUT            ;Send it out
  140. ;
  141. ;Second and third octal digits have 3 bits each
  142.       MOV     CX,2               ;Loop for a count of 2
  143. OCT8OUT1:
  144.       PUSH     CX               ;Save the count
  145.       MOV     CL,3               ;For a count of 3
  146.       ROL     DL,CL               ;Rotate DL left
  147.       MOV     AL,DL               ;Move into AL
  148.       AND     AL,7               ;Keep just digit
  149.       ADD     AL,30H            ;Add 30H to AL
  150.       CALL     STDOUT            ;Send it out
  151.       POP     CX               ;Restore count
  152.       LOOP     OCT8OUT1
  153.       POP     AX               ;Restore registers
  154.       POP     CX
  155.       RET                   ;Return
  156. OCT8OUT ENDP
  157. ;-----------------------------------------------------------------------------
  158. ;  Routine to convert from internal 16-bit binary to ASCII octal
  159. ;
  160. OCT16OUT  PROC     FAR
  161. ;
  162. ;A binary number is in DX
  163.       PUSH     CX               ;Save registers
  164.       PUSH     AX
  165. ;
  166. ;First octal digit has only one bit
  167.       ROL     DX,1               ;Rotate DX left one
  168.       MOV     AL,DL               ;Move into AL
  169.       AND     AL,1               ;Just keep digit
  170.       ADD     AL,30H            ;Add 30H to AL
  171.       CALL     STDOUT            ;Send it out
  172. ;
  173. ;Last five octal digits have 3 bits each
  174.       MOV     CX,5               ;Loop for a count of 5
  175. OCT16OUT1:
  176.       PUSH     CX               ;Save count
  177.       MOV     CL,3               ;For a count of 3
  178.       ROL     DX,CL               ;Rotate DL left
  179.       MOV     AL,DL               ;Move into AL
  180.       AND     AL,7               ;Keep just digit
  181.       ADD     AL,30H            ;Add 30H to AL
  182.       CALL     STDOUT            ;Send it out
  183.       POP     CX               ;Restore count
  184.       LOOP     OCT16OUT1
  185.       POP     AX               ;Restore registers
  186.       POP     CX
  187.       RET                   ;Return
  188. OCT16OUT  ENDP
  189. ;-----------------------------------------------------------------------------
  190. ;Routine to convert from ASCII hexadecimal to internal 16-bit binary
  191. ;
  192. HEX16IN   PROC     FAR
  193.       PUSH     CX               ;Save registers
  194.       PUSH     AX
  195.       MOV     DX,0               ;Initialize DX as 0
  196. HEX16IN1:
  197.       CALL     STDIN               ;A digit comes in AL
  198.       SUB     AL,30H            ;Subtract 30H
  199.       JL     HEX16IN3           ;Check if too low
  200.       CMP     AL,9
  201.       JLE     HEX16IN2           ;Go if OK
  202.       AND     AL,5Fh            ;For lower case too
  203.       SUB     AL,7               ;Adjust for A-F
  204.       JL     HEX16IN3           ;Too low for A-F
  205.       CMP     AL,15               ;Check if too high
  206.       JG     HEX16IN3
  207. HEX16IN2:
  208.       CBW
  209.       MOV     CL,4               ;For count of 4
  210.       SAL     DX,CL               ;Shift DX left
  211.       ADD     DX,AX               ;Add in digit
  212.       JMP     HEX16IN1
  213. HEX16IN3:
  214.       POP     AX               ;Restore registers
  215.       POP     CX
  216.       RET                   ;Return
  217. HEX16IN   ENDP
  218. ;-----------------------------------------------------------------------------
  219. ;Routine to convert from internal 8-bit binary to ASCII hexadecimal
  220. ;
  221. HEX8OUT   PROC     FAR
  222. ;
  223. ;A binary number is in DL
  224.       PUSH     CX               ;Save registers
  225.       PUSH     AX
  226.       MOV     CX,2               ;Loop for a count of 2
  227. HEX8OUT1:
  228.       PUSH     CX               ;Save the count
  229.       MOV     CL,4               ;For a count of 4
  230.       ROL     DL,CL               ;Rotate DL left
  231.       MOV     AL,DL               ;Move into AL
  232.       AND     AL,00FH           ;Just digit
  233.       DAA                   ;Add 6 if A-F  DLP
  234.       ADD     AL,0F0H           ;Bump a carry if A-F
  235.       ADC     AL,040H           ;Here is the ASCII
  236.       CALL     STDOUT            ;Send it out
  237.       POP     CX
  238.       LOOP     HEX8OUT1
  239.       POP     AX               ;Restore registers
  240.       POP     CX
  241.       RET                   ;Return
  242. HEX8OUT   ENDP
  243. ;------------------------------------------------------------------------------
  244. ;Routine to convert from internal 16-bit binary to ASCII hexadecimal
  245. ;
  246. HEX16OUT  PROC FAR
  247. ;
  248. ;A binary number is in DX
  249.       PUSH     CX